/**
 * gpms
 * 2012-8-20 下午5:33:37
 * 作者：陈志明
 * 
 */
package com.gpms.privilege.action;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.gpms.common.action.BaseActionSupport;
import com.gpms.common.util.Constant;
import com.gpms.common.util.ListForJson;
import com.gpms.mapper.*;
import com.gpms.privilege.service.UserService;

/**
 * @author Jackie chen
 *
 */
public class UserAction extends BaseActionSupport {
	private Users user;
	private List<Users> userList;
	private List<ItemDetails> itemDetailsList;
	private String sign;
	private String jsonResults;
	private String message;
	
	public String getUserAll() throws Exception{
		userList=userService.getAllUser();
//		jsonResults=ListForJson.doListJson(1, 20, userList);
//		System.out.println(jsonResults);
//		response.getWriter().write(jsonResults);
		return "userList";
	}
	
	public String getTestGrid() throws Exception{
		userList=userService.getAllUser();
		jsonResults=ListForJson.doListJson(1, 20, userList);
		System.out.println(jsonResults);
//		response.getWriter().write(jsonResults);
		return "testGrid";
	}
	
	/**
	 * 编辑用户
	 * @return
	 */
//	public String editUser(){
//		itemDetailsList=itemDetailsService.getItemDetailsByDetailsType("enable");
//		if(StringUtils.equals(sign, "update")){
//			user=userService.getUserById(user);
//		}
//		return "editUser";
//	}
	
	/**
	 * 查看用户
	 * @return
	 */
	public String checkUser(){
		itemDetailsList=itemDetailsService.getItemDetailsByDetailsType("enable");
		user=userService.getUserById(user);
		return "checkUser";
	}
	
	public String addUser_view(){
		itemDetailsList=itemDetailsService.getItemDetailsByDetailsType("enable");
		return "addUser";
	}
	
	/**
	 * 添加用户
	 * @param user
	 * @return
	 */
	public String addUser(Users user){
		itemDetailsList=itemDetailsService.getItemDetailsByDetailsType("enable");
		//判断用户是否存在
		if(userService.getUserByName(user)==null){
			userService.addUser(user);
			message="用户添加成功";
		}else{
			message="用户已存在";
		}
		return "addUser";
	}
	
	public String updateUser_View(){
		itemDetailsList=itemDetailsService.getItemDetailsByDetailsType("enable");
		user=userService.getUserById(user);
		return "updateUser";
	}
	
	/**
	 * 修改用户
	 * @param user
	 * @return
	 */
	public String updateUser(Users user){
		itemDetailsList=itemDetailsService.getItemDetailsByDetailsType("enable");
		if(!StringUtils.equals(user.getUsername(), Constant.SYSTEM)){
			int i=userService.updateUserById(user);
			message="修改成功";
		}else{
			message="没有权限修改用户";
		}
		return "updateUser";
	}
	
	/**
	 * 删除用户
	 * @param user
	 * @return
	 */
	public String deleteUser(Users user){
		userService.deleteUserById(user);
		return "deleteUser";
	}
	
	/**
	 * 批量删除用户
	 * @param ids
	 * @return
	 */
	public String deleteUser(String ids){
		userService.deleteUserByIds(ids);
		return "deleteUsers";
	}
	
	////////////////////////////////////////////
	
	/**
	 * @return the user
	 */
	public Users getUser() {
		return user;
	}
	/**
	 * @param user the user to set
	 */
	public void setUser(Users user) {
		this.user = user;
	}
	/**
	 * @return the userList
	 */
	public List getUserList() {
		return userList;
	}

	/**
	 * @return the itemDetailsList
	 */
	public List getItemDetailsList() {
		return itemDetailsList;
	}

	/**
	 * @return the sign
	 */
	public String getSign() {
		return sign;
	}

	/**
	 * @param sign the sign to set
	 */
	public void setSign(String sign) {
		this.sign = sign;
	}

	/**
	 * @return the jsonResults
	 */
	public String getJsonResults() {
		return jsonResults;
	}

	/**
	 * @param jsonResults the jsonResults to set
	 */
	public void setJsonResults(String jsonResults) {
		this.jsonResults = jsonResults;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}
}
